export function isEmpty(input) {
  return input == null || input == ''
}
export function isNotEmpty(input) {
  return !this.isEmpty(input)
}
export function isBlank(input) {
  return input == null || /^\s*$/.test(input)
}
export function isNotBlank(input) {
  return !this.isBlank(input)
}
export function trim(input) {
  return input.replace(/^\s+|\s+$/, '')
}
export function trimToEmpty(input) {
  return input == null ? '' : this.trim(input)
}
export function startsWith(input, prefix) {
  return input.indexOf(prefix) === 0
}
export function endsWith(input, suffix) {
  var regex = new RegExp(this.escapeMetacharacterOfStr(suffix) + '$')
  return regex.test(input)
}
export function contains(input, searchSeq) {
  return input.indexOf(searchSeq) >= 0
}
export function equals(input1, input2) {
  return input1 == input2
}
export function equalsIgnoreCase(input1, input2) {
  return input1.toLocaleLowerCase() == input2.toLocaleLowerCase()
}
export function containsWhitespace(input) {
  return this.contains(input, ' ')
}
//生成指定个数的字符
export function repeat(ch, repeatTimes) {
  var result = ''
  for (var i = 0; i < repeatTimes; i++) {
    result += ch
  }
  return result
}
export function deleteWhitespace(input) {
  return input.replace(/\s+/g, '')
}
export function rightPad(input, size, padStr) {
  return input + this.repeat(padStr, size)
}
export function leftPad(input, size, padStr) {
  return this.repeat(padStr, size) + input
}
//首小写字母转大写
export function capitalize(input) {
  var strLen = 0
  if (input == null || (strLen = input.length) == 0) {
    return input
  }
  return input.replace(/^[a-z]/, function(matchStr) {
    return matchStr.toLocaleUpperCase()
  })
}
//首大写字母转小写
export function uncapitalize(input) {
  var strLen = 0
  if (input == null || (strLen = input.length) == 0) {
    return input
  }
  return input.replace(/^[A-Z]/, function(matchStr) {
    return matchStr.toLocaleLowerCase()
  })
}
//大写转小写，小写转大写
export function swapCase(input) {
  return input.replace(/[a-z]/gi, function(matchStr) {
    if (matchStr >= 'A' && matchStr <= 'Z') {
      return matchStr.toLocaleLowerCase()
    } else if (matchStr >= 'a' && matchStr <= 'z') {
      return matchStr.toLocaleUpperCase()
    }
  })
}
//统计含有的子字符串的个数
export function countMatches(input, sub) {
  if (this.isEmpty(input) || this.isEmpty(sub)) {
    return 0
  }
  var count = 0
  var index = 0
  while ((index = input.indexOf(sub, index)) != -1) {
    index += sub.length
    count++
  }
  return count
}
//只包含字母
export function isAlpha(input) {
  return /^[a-z]+$/i.test(input)
}
//只包含字母、空格
export function isAlphaSpace(input) {
  return /^[a-z\s]*$/i.test(input)
}
//只包含字母、数字
export function isAlphanumeric(input) {
  return /^[a-z0-9]+$/i.test(input)
}
//只包含字母、数字和空格
export function isAlphanumericSpace(input) {
  return /^[a-z0-9\s]*$/i.test(input)
}
//数字
export function isNumeric(input) {
  return /^(?:[1-9]\d*|0)(?:\.\d+)?$/.test(input)
}
//小数
export function isDecimal(input) {
  return /^[-+]?(?:0|[1-9]\d*)\.\d+$/.test(input)
}
//负小数
export function isNegativeDecimal(input) {
  return /^\-(?:0|[1-9]\d*)\.\d+$/.test(input)
}
//正小数
export function isPositiveDecimal(input) {
  return /^\+?(?:0|[1-9]\d*)\.\d+$/.test(input)
}
//整数
export function isInteger(input) {
  return /^[-+]?(?:0|[1-9]\d*)$/.test(input)
}
//正整数
export function isPositiveInteger(input) {
  return /^\+?(?:0|[1-9]\d*)$/.test(input)
}
//负整数
export function isNegativeInteger(input) {
  return /^\-(?:0|[1-9]\d*)$/.test(input)
}
//只包含数字和空格
export function isNumericSpace(input) {
  return /^[\d\s]*$/.test(input)
}
export function isWhitespace(input) {
  return /^\s*$/.test(input)
}
export function isAllLowerCase(input) {
  return /^[a-z]+$/.test(input)
}
export function isAllUpperCase(input) {
  return /^[A-Z]+$/.test(input)
}
export function defaultString(input, defaultStr) {
  return input == null ? defaultStr : input
}
export function defaultIfBlank(input, defaultStr) {
  return this.isBlank(input) ? defaultStr : input
}
export function defaultIfEmpty(input, defaultStr) {
  return this.isEmpty(input) ? defaultStr : input
}
//字符串反转
export function reverse(input) {
  if (this.isBlank(input)) {
    input
  }
  return input
    .split('')
    .reverse()
    .join('')
}
//删掉特殊字符(英文状态下)
export function removeSpecialCharacter(input) {
  return input.replace(/[!-/:-@\[-`{-~]/g, '')
}

//只包含特殊字符、数字和字母（不包括空格，若想包括空格，改为[ -~]）
export function isSpecialCharacterAlphanumeric(input) {
  return /^[!-~]+$/.test(input)
}
/**
 * @param {String} message
 * @param {Array} arr
 * 消息格式化
 */
export function format(message, arr) {
  return message.replace(/{(\d+)}/g, function(matchStr, group1) {
    return arr[group1]
  })
}
/**
 * 把连续出现多次的字母字符串进行压缩。如输入:aaabbbbcccccd  输出:3a4b5cd
 * @param {String} input
 * @param {Boolean} ignoreCase : true or false
 */
export function compressRepeatedStr(input, ignoreCase) {
  var pattern = new RegExp('([a-z])\\1+', ignoreCase ? 'ig' : 'g')
  return (result = input.replace(pattern, function(matchStr, group1) {
    return matchStr.length + group1
  }))
}
//中文校验
export function isChinese(input) {
  return /^[\u4E00-\u9FA5]+$/.test(input)
}
//去掉中文字符
export function removeChinese(input) {
  return input.replace(/[\u4E00-\u9FA5]+/gm, '')
}
//转义元字符
export function escapeMetacharacter(input) {
  var metacharacter = '^$()*+.[]|\\-?{}'
  if (metacharacter.indexOf(input) >= 0) {
    input = '\\' + input
  }
  return input
}

//转义字符串中的元字符
export function escapeMetacharacterOfStr(input) {
  return input.replace(/[-$^()*+.\[\]|\\?{}]/gm, '\\$&')
}
//中文转为unicode编码
export function chineseToUnicode(input) {
  return input.replace(/[\u4E00-\u9FA5]/g, function(matchStr) {
    return '\\u' + matchStr.charCodeAt(0).toString(16)
  })
}
/**
 * 自定义嵌套符号rule={'(':')','[':']','{':'}','<':'>'};
 * @param {Object} rule
 * @param {String} str
 */
export function isNest(rule, str) {
  if (!(rule && str)) {
    return false
  }
  var keys = []
  var values = []
  for (var key in rule) {
    if (rule.hasOwnProperty(key)) {
      keys.push(key)
      values.push(rule[key])
    }
  }
  var chs = str.split('')
  var len = chs.length
  var stack = []
  for (var i = 0; i < len; i++) {
    if (Bee.ArrayUtils.inArray(keys, chs[i])) {
      stack.push(rule[chs[i]])
    } else {
      if (chs[i] === stack[stack.length - 1]) {
        stack.pop()
      } else if (Bee.ArrayUtils.inArray(values, chs[i])) {
        return false
      }
    }
  }
  return stack.length === 0
}
//密码校验 必须包含数字、英文字母、特殊符号且大于等于8位（特殊符号包括: ~!@#$%^&*）
export function password(input) {
  return /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/.test(input)
//   return /(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,30}/.test(input)
}
export default {
  isEmpty,
  isNotEmpty,
  isBlank,
  isNotBlank,
  trim,
  trimToEmpty,
  startsWith,
  endsWith,
  contains,
  equals,
  equalsIgnoreCase,
  containsWhitespace,
  repeat,
  deleteWhitespace,
  rightPad,
  leftPad,
  capitalize,
  uncapitalize,
  swapCase,
  countMatches,
  isAlpha,
  isAlphaSpace,
  isAlphanumeric,
  isAlphanumericSpace,
  isNumeric,
  isDecimal,
  isNegativeDecimal,
  isPositiveDecimal,
  isInteger,
  isPositiveInteger,
  isNegativeInteger,
  isNumericSpace,
  isWhitespace,
  isAllLowerCase,
  isAllUpperCase,
  defaultString,
  defaultIfBlank,
  defaultIfEmpty,
  reverse,
  removeSpecialCharacter,
  isSpecialCharacterAlphanumeric,
  format,
  compressRepeatedStr,
  isChinese,
  removeChinese,
  escapeMetacharacter,
  escapeMetacharacterOfStr,
  chineseToUnicode,
  isNest,
  password
}
